<template>
  <div id="self_bartending_devicelDetail">
    <c-title :hide="false" text="设备详情"></c-title>
    <diyBackGround :minHeight="'8.5rem'">
      <div class="devicelDetail" v-if="info">
        <div class="devicelDetail_left">
          <div class="deviclCode">设备编码：{{ info.device_sn }}</div>
          <div class="deviclOnline">上线时间：{{ info.online_time }}</div>
        </div>
        <div class="devicelDetail_right">
          <img :src="Imgurl" alt="" />
        </div>
      </div>
    </diyBackGround>
    <div class="contenBox" v-if="info">
      <div class="nav">
        <van-tabs v-model="active" line-width="15px" title-active-color="#F15353" @change="tabChange">
          <van-tab title="设备状态" title-style="font-weight: 500; font-size: 0.88rem;"></van-tab>
          <van-tab title="存储基酒" title-style="font-weight: 500; font-size: 0.88rem;"></van-tab>
          <van-tab title="登录用户" title-style="font-weight: 500; font-size: 0.88rem;"></van-tab>
          <van-tab title="设备订单" title-style="font-weight: 500; font-size: 0.88rem;"></van-tab>
        </van-tabs>
      </div>
      <div class="navTitle" v-if="active == 0 || active == 1">{{ setNavTitle }}</div>
      <template v-if="active == 0">
        <div class="statusBox">
          <div class="flex">
            <div class="rowBox dfwidth">
              <div class="txt">{{ info.signal }}</div>
              <div class="btxt">信号强度</div>
            </div>
            <div class="rowBox">
              <div class="txt">{{ info.use_status_name }}</div>
              <div class="btxt">使用状态</div>
            </div>
          </div>
          <div class="flex" style="flex-wrap: wrap;">
            <div class="rowBoxMini" @click="commandDevice('disinfect_status')" v-if="check_permission('5')">
              <div class="txt">{{ info.disinfect_status_name }}</div>
              <div class="btxt">消毒状态</div>
            </div>
            <div class="rowBoxMini" @click="commandDevice('table_status')" v-if="check_permission('9')">
              <div class="txt">{{ info.table_status_name }}</div>
              <div class="btxt">升降台状态</div>
            </div>
            <div class="rowBoxMini" @click="commandDevice('aged_wine_drawer_status')" v-if="check_permission('10')">
              <div class="txt"></div>
              <div class="btxt">老酒抽屉</div>
            </div>
          </div>
          <div class="tips">可点击按钮进行操作</div>
        </div>
        <div class="navTitle">基酒存量</div>
        <div class="totalList">
          <div class="list flist">
            <div class="lis">酒桶名称</div>
            <div class="lis">存酒量</div>
            <div class="lis">出酒量</div>
            <div class="lis" style="color: #000;">剩余量</div>
          </div>
          <div class="list" v-for="(item, index) in info.has_many_barrel" :key="index">
            <div class="lis">{{ item.label }}</div>
            <div class="lis">{{ item.total }}</div>
            <div class="lis">{{ item.used }}</div>
            <div class="lis" :class="{ lis_red: item.is_warning }">{{ item.left }}</div>
          </div>
        </div>
      </template>
      <template v-if="active == 1">
        <template v-for="(item, index) in info.has_many_barrel">
          <div class="classify" :key="index">
            <div class="classify_l">
              <span>酒桶{{ index + 1 }}</span>
              <i class="iconfont icon-fontclass-gengxin" @click="to_canning" v-if="check_permission('8')"></i>
            </div>
            <div class="classify_r">{{ item.label }}</div>
          </div>
        </template>
      </template>
      <template v-if="active == 2">
        <div class="userList" @click="to_self_bartending_bartendUserDetail(item.uid)" v-for="item in listData" :key="item.uid">
          <img :src="item.avatar_image" class="userList_img" alt="" />
          <div class="userList_c">
            <div class="userList_c_t">
              <div class="name">{{ item.nickname }}</div>
            </div>
            <div class="userList_c_m" v-if="item.mobile">{{ item.mobile }}</div>
            <div class="userList_c_time">{{ item.first_time }}</div>
          </div>
          <div class="userList_r">
            <div class="userList_r_order">
              <div class="userList_r_order_num">{{ item.total_count }}</div>
              <div class="userList_r_order_txt">订单数</div>
            </div>
            <i class="iconfont icon-icon_more11"></i>
          </div>
        </div>
      </template>
      <template v-if="active == 3">
        <van-search v-model="keyword" @search="childChangeTab" placeholder="搜索订单" background="#f5f5f5" shape="round">
          <template slot="left-icon">
            <i class="iconfont icon-sousuo1"></i>
          </template>
        </van-search>
        <van-tabs v-model="active_child" background="#f5f5f5" line-width="20px" @change="childChangeTab">
          <van-tab :title="item" v-for="(item, index) in childNavArr" :key="index"></van-tab>
        </van-tabs>
        <bartendingOrderList @refreshUpdate="refreshUpdate" :orderItem="listData"></bartendingOrderList>
      </template>
    </div>
    <div v-if="active == 1" class="posBox set-pc-style">
      <div class="btn" @click="to_canning" v-if="check_permission('8')">
        <i class="iconfont icon-fontclass-gengxin"></i>
        <span>灌装</span>
      </div>
      <div class="btn btn_r" @click="to_mixology" v-if="info.can_order">
        <span>去调酒</span>
      </div>
    </div>
  </div>
</template>
<script>
import diyBackGround from "../../../components/diyBackGround.vue";
import bartendingOrderList from "../components/bartendingOrderList.vue";
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      active: 0,
      Imgurl: window.location.origin + "/static/local/self-bartending/device.png",
      active_child: 0,
      childNavArr: ["全部", "待付款", "待发货", "待收货", "已完成", "已取消", "已退款"],
      info: null,
      keyword: "",

      listData: [],
      page: 1,
      total_page: 0,
      isLoadMore: true
    };
  },
  activated() {
    this.active = 0;
    this.active_child = 0;
    this.init();
    this.keyword = "";
    this.getData();
  },
  methods: {
    refreshUpdate() {
      this.init();
      this.getTabList();
    },
    // 导航切换
    tabChange() {
      this.keyword = "";
      if (this.active == 2 || this.active == 3) {
        this.init();
        this.initChild();
        this.getTabList();
      }
    },
    // 订单子导航切换
    childChangeTab() {
      this.init();
      this.getTabList();
    },
    init() {
      this.page = 1;
      this.total_page = 0;
      this.listData = [];
    },
    initChild() {
      this.active_child = 0;
    },
    async getTabList() {
      let json = {
        device_id: this.$route.params.id
      };
      let _url = "";
      if (this.active == 2) {
        _url = "plugin.self-bartending.api.employee.login-log.index";
      } else {
        json.keyword = this.keyword;
        _url = "plugin.self-bartending.api.employee.order.index";
        if (this.active_child == 1) {
          _url = "plugin.self-bartending.api.employee.order.waitPay";
        } else if (this.active_child == 2) {
          _url = "plugin.self-bartending.api.employee.order.waitSend";
        } else if (this.active_child == 3) {
          _url = "plugin.self-bartending.api.employee.order.waitReceive";
        } else if (this.active_child == 4) {
          _url = "plugin.self-bartending.api.employee.order.completed";
        } else if (this.active_child == 5) {
          _url = "plugin.self-bartending.api.employee.order.canceled";
        } else if (this.active_child == 6) {
          _url = "plugin.self-bartending.api.employee.order.refunded";
        }
      }
      let { data, msg, result } = await $http.post(_url, json, "");
      if (result) {
        this.listData = data.data;
        this.isLoadMore = true;
        this.total_page = data.last_page;
      } else {
        this.$toast(msg);
      }
    },
    async getMoreData() {
      if (!this.isLoadMore) return;
      this.isLoadMore = false;
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let json = {
          page: this.page,
          device_id: this.$route.params.id,
          page: this.page 
        };
        let _url = "";
        if (this.active == 2) {
          _url = "plugin.self-bartending.api.employee.login-log.index";
        } else {
          this.active_child = 0;
          json.device_id = this.$route.params.id;
          json.keyword = this.keyword;
          _url = "plugin.self-bartending.api.employee.order.index";
          if (this.active_child == 1) {
            _url = "plugin.self-bartending.api.employee.order.waitPay";
          } else if (this.active_child == 2) {
            _url = "plugin.self-bartending.api.employee.order.waitSend";
          } else if (this.active_child == 3) {
            _url = "plugin.self-bartending.api.employee.order.waitReceive";
          } else if (this.active_child == 4) {
            _url = "plugin.self-bartending.api.employee.order.completed";
          } else if (this.active_child == 5) {
            _url = "plugin.self-bartending.api.employee.order.canceled";
          } else if (this.active_child == 6) {
            _url = "plugin.self-bartending.api.employee.order.refunded";
          }
        }
        let { data, msg, result } = await $http.post(_url, json, "");
        this.isLoadMore = true;
        if (result) {
          this.listData = this.listData.concat(data.data);
        } else {
          this.$toast(msg);
          this.page = this.page - 1;
          this.isLoadMore = false;
        }
      }
    },
    to_canning() {
      this.$router.push(this.fun.getUrl("self_bartending_canning", { id: this.$route.params.id }));
    },
    to_mixology() {
      this.$router.push(this.fun.getUrl("self_bartending_mixology", { id: this.$route.params.id }));
    },
    to_self_bartending_bartendUserDetail(uid) {
      this.$router.push(this.fun.getUrl("self_bartending_bartendUserDetail", { id: uid }));
    },
    async getData() {
      let { data, msg, result } = await $http.post("plugin.self-bartending.api.employee.device.info", { id: this.$route.params.id }, "");
      if (result) {
        this.info = data;
      } else {
        this.$toast(msg);
      }
    },
    async commandDevice(key) {
      let json = {
        id: this.$route.params.id
      };
      json.switch = this.info[key] == 0 ? "open" : "close";
      if (key == "disinfect_status") {
        json.action = "disinfect";
      } else if (key == "table_status") {
        json.action = "table";
      } else if (key == "aged_wine_drawer_status") {
        json.action = "drawer";
      }
      let { data, msg, result } = await $http.post("plugin.self-bartending.api.employee.device.command", json, "");
      if (result) {
        this.$toast(msg);
        this.getData();
      } else {
        this.$toast(msg);
      }
    }
  },
  computed: {
    setNavTitle() {
      if (this.active == 0) {
        return "相关状态";
      } else if (this.active == 1) {
        return "基酒种类";
      }
    },
    check_permission(status) {
      return function(status) {
        if (this.info && this.info.permission) {
          return this.info.permission.includes(status);
        }
        return false;
      };
    }
  },
  components: {
    diyBackGround,
    bartendingOrderList
  }
};
</script>
<style lang="scss" scoped>
#self_bartending_devicelDetail {
  .van-search {
    padding: 0;
    margin-top: 0.63rem;
    .van-search__content {
      background: #ffffff;
    }
  }
  .posBox {
    position: fixed;
    width: 100%;
    background: #fff;
    bottom: 0;
    left: 0;
    padding: 0.63rem 1.25rem;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    .btn {
      width: 10rem;
      padding: 0.7rem 0;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      background: #ffffff;
      border-radius: 1.25rem;
      border: 0.03rem solid #f15353;
      color: #f15353;
      margin-right: 0.94rem;
      .icon-fontclass-gengxin {
        margin-right: 0.3rem;
      }
    }
    .btn_r {
      margin-right: 0;
      background: #f15353;
      color: #fff;
      border: 0.03rem solid #fff;
    }
  }
  .contenBox {
    margin: 0 0.75rem;
    margin-top: -2.5rem;
    padding-bottom: 2rem;
    .nav {
      border-radius: 0.5rem !important;
      overflow: hidden;
      padding-bottom: 0.3rem;
      background: #fff;
    }
    .navTitle {
      margin: 1.03rem 0 0.84rem 0.28rem;
      font-weight: bold;
      font-size: 0.88rem;
      line-height: 0.9rem;
      color: #00001c;
      text-align: left;
    }
    .userList {
      display: flex;
      margin-top: 0.63rem;
      padding: 0.75rem;
      box-sizing: border-box;
      border-radius: 0.5rem;
      background: #ffffff;
      .userList_img {
        width: 3.5rem;
        height: 3.5rem;
        background: #d6d6dc;
        border-radius: 50%;
        margin: 0;
        margin-right: 0.5rem;
        flex-shrink: 0;
      }
      .userList_c {
        flex: 1;
        text-align: left;
        overflow: hidden;
        .userList_c_t {
          display: flex;
          align-items: center;
          margin-bottom: 0.28rem;
          .name {
            font-weight: bold;
            font-size: 0.94rem;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          .status {
            flex-shrink: 0;
            padding-left: 0.63rem;
            padding-right: 0.28rem;
            box-sizing: border-box;
            font-size: 0.6rem;
            line-height: 0.94rem;
            width: 2.28rem;
            height: 0.94rem;
            color: #14b237;
            background: #d0f0d7;
            margin-left: 0.37rem;
            border-radius: 0.25rem;
            position: relative;
          }
          .status::after {
            content: "";
            position: absolute;
            left: 0.25rem;
            top: 0.35rem;
            width: 0.19rem;
            height: 0.19rem;
            background: #14b237;
            border-radius: 50%;
          }
        }
        .userList_c_m {
          font-size: 0.81rem;
          margin-bottom: 0.28rem;
          line-height: 1rem;
        }
        .userList_c_time {
          font-size: 0.81rem;
          line-height: 1rem;
          color: #999999;
        }
      }
      .userList_r {
        flex-shrink: 0;
        display: flex;
        align-items: center;
        .icon-icon_more11 {
          color: #aaaab3;
          margin-left: 0.84rem;
        }
        .userList_r_order {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          .userList_r_order_num {
            font-weight: bold;
            font-size: 0.94rem;
          }
          .userList_r_order_txt {
            font-size: 0.81rem;
            color: #999999;
          }
        }
      }
    }
    .classify {
      padding: 1.06rem 0.63rem;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      height: 3rem;
      background: #ffffff;
      border-radius: 0.5rem;
      align-items: center;
      font-size: 0.88rem;
      margin-bottom: 0.63rem;
      .classify_l {
        font-weight: 500;
        flex: 1;
        display: flex;
        align-items: center;
        .icon-fontclass-gengxin {
          color: #f15353;
          margin-left: 0.41rem;
          font-size: 0.88rem;
        }
      }
      .classify_r {
        flex: 1;
        text-align: right;
      }
    }
    .classify:last-child {
      margin-bottom: 0;
    }
    .statusBox {
      background: #ffffff;
      border-radius: 0.5rem;
      padding: 0.63rem;
      box-sizing: border-box;
      .tips {
        font-size: 0.75rem;
        color: #999999;
        text-align: left;
        margin-top: 0.63rem;
      }
      .rowBox {
        flex: 1;
        height: 4.06rem;
        background: #f5f5f5;
        border-radius: 0.5rem;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        .txt {
          font-weight: 500;
          font-size: 0.88rem;
          color: #00001c;
        }
        .btxt {
          font-size: 0.75rem;
          color: #999999;
        }
      }
      .rowBoxMini {
        width: 6.47rem;
        height: 4.06rem;
        margin-right: 0.63rem;
        background: #f5f5f5;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 0.5rem;
        flex-direction: column;
        margin-top: 0.63rem;
        .txt {
          font-weight: 500;
          font-size: 0.88rem;
          color: #f15353;
        }
        .btxt {
          font-size: 0.75rem;
          color: #999999;
        }
      }
      .rowBoxMini:nth-child(3n) {
        margin-right: 0;
      }

      .dfwidth {
        margin-right: 0.66rem;
      }
    }
    .totalList {
      background: #fff;
      border-radius: 0.5rem;
      padding-bottom: 0.35rem;
      .list {
        display: flex;
        padding: 0 0.44rem;
        // margin: 0.31rem;
        height: 3rem;
        line-height: 3rem;
      }
      .flist {
        height: 2.75rem;
        line-height: 2.75rem;
      }
      .flist .lis {
        font-weight: 500;
        font-size: 0.88rem;
        color: #00001c;
      }
      .list:nth-child(2n) {
        background: #f4f4f6;
        margin: 0 0.3rem;
      }
      .lis {
        flex: 0.3;
        text-align: left;
        overflow: hidden;
      }
      .lis:first-child {
        flex: 0.4;
      }
      .lis_red {
        color: #f15353;
      }
    }
  }
  .devicelDetail {
    margin: 1.91rem 1.03rem;
    display: flex;
    justify-content: space-between;

    .devicelDetail_left {
      text-align: left;
      .deviclCode {
        font-size: 1rem;
        color: #ffffff;
        font-weight: bold;
      }
      .deviclOnline {
        font-size: 0.81rem;
        color: #ffffff;
        margin-top: 0.56rem;
      }
    }
    .devicelDetail_right {
      width: 5.34rem;
      height: 6.09rem;
      img {
        width: 5.34rem;
        height: 6.09rem;
      }
    }
  }
}
</style>
